%%% Gait detection data analysis %%%
close all;
clearX('data');
clc;


%% Defining Constants
global data
if isempty(data)
    data = [];
    sn = 1;
else
    sn = length(data)+1;
end
subject = 'JC';
filename = 'block1_2km';
height = 74 * 0.0254; %inches to meters
weight = 190 / 2.2; %lbs. to kg
DOB = '?';
catch_trials = [5 9 13 16];
baseline = [1 2];
washout = [18 19 20 21 22];

data(sn).subject_data.name = subject;
data(sn).subject_data.filename = filename;
data(sn).subject_data.height = height;
data(sn).subject_data.weight = weight;
data(sn).subject_data.DOB = DOB;
data(sn).subject_data.catch_trials = catch_trials;
data(sn).subject_data.baseline = baseline;
data(sn).subject_data.washout = washout;

for trial = 1:22
    clearX('data','filename','trial','sn','weight','catch_trials','baseline','washout');
    % enter filename 'Block_X_YKMZ' as a filename for othosis case, enter
    % 'no_orthosis_Y_walkZ' for no orthosis case
    plate_length = 1.00; %m
    plate_width = 0.30; %m
    plate_offset = 0.30; %m
    force_scale = 0.03; % scaling GRFs for visualization
    force_filter = 8; %Hz
    emg_filter_1 = 120; %Hz
    emg_filter_2 = 10; %Hz
    MA_filter = 8;%Hz
    LKnee_offset = 0.07;
    foot_width = 0.09;
    ankle_size = 0.04;
    knee_size = 0.05;
    hip_size = 0.05;
    leg_radius = 0.04;
    thigh_radius = 0.05;
    pelvis_height = 0.08;
    foot_height = 0.04;
    foot_scale = 2;

    %% Loading files
    % load .anc file
[labels_anc,time_anc,EMG,rel_angle,step_number,forces] = readColDataANC(strcat(filename,num2str(trial),'.anc'),23,11,1);

    % load .trc file
    [labels,time_trc,LASIS,RASIS,Sacrum,Sac_offset,LThigh_up,LThigh_front,LThigh_rear,...
        LShank_up, LShank_front, LShank_rear, LAnkle_lat,  LHeel, LToe, RThigh_up, RThigh_front,RThigh_rear,...  %switched RAnkle_lat and LAnkle_lat for no-orthosis_2km1
        RShank_up, RShank_front, RShank_rear, RAnkle_lat,RHeel, RToe]...
        = readColDataMA_dyn(strcat(filename,num2str(trial),'.trc'),68,6,1);
    % [time_trc,Rtoe,Rankle,Rheel,Ltoe,Lankle,Lheel] =
    % readColDataXLS_MA(filename);

    %% analyzing data
    dt_trc = time_trc(2)-time_trc(1);
    dt_anc = time_anc(2)-time_anc(1);
    [b,a] = butter(4,8/500); forces = filtfilt(b,a,forces);
    Lforce = forces(:,13:16)./204.8;
    Lforce_totalz = sum(Lforce,2)+.00001;
    Rforce = forces(:,5:8)./204.8;
    Rforce_totalz = sum(Rforce,2)+.00001;

    Lforce_x = forces(:,9:10)./204.8;
    Lforce_totalx = sum(Lforce_x,2);
    Lforce_y = forces(:,11:12)./204.8;
    Lforce_totaly = sum(Lforce_y,2);
    Lforce_mag = sqrt(Lforce_totalx.^2 + Lforce_totaly.^2 + Lforce_totalz.^2);
    [b,a] = butter(4,force_filter/500); Lforce_mag = filtfilt(b,a,Lforce_mag);
    Lforce_dir = [Lforce_totalx./Lforce_mag Lforce_totaly./Lforce_mag Lforce_totalz./Lforce_mag];
    [b,a] = butter(4,force_filter/500); Lforce_dir = filtfilt(b,a,Lforce_dir);

    Rforce_x = forces(:,1:2)./204.8;
    Rforce_totalx = sum(Lforce_x,2);
    Rforce_y = forces(:,3:4)./204.8;
    Rforce_totaly = sum(Lforce_y,2);
    Rforce_mag = sqrt(Rforce_totalx.^2 + Rforce_totaly.^2 + Rforce_totalz.^2);
    [b,a] = butter(4,force_filter/500); Rforce_mag = filtfilt(b,a,Rforce_mag);
    Rforce_dir = [Rforce_totalx./Rforce_mag Rforce_totaly./Rforce_mag Rforce_totalz./Rforce_mag];
    [b,a] = butter(4,force_filter/500); Rforce_dir = filtfilt(b,a,Rforce_dir);

    Rforce_mag_r = force_scale*Rforce_mag; Lforce_mag_l = force_scale*Lforce_mag;

    CoP_L = [(Lforce(:,1)+Lforce(:,2))./Lforce_totalz * plate_length  (Lforce(:,1)+Lforce(:,4))./Lforce_totalz * plate_width + plate_offset];
    CoP_R = [(Rforce(:,1)+Rforce(:,2))./Rforce_totalz * plate_length  (Rforce(:,1)+Rforce(:,4))./Rforce_totalz * plate_width];
    [b,a] = butter(4,force_filter/500); CoP_L = filtfilt(b,a,CoP_L);
    [b,a] = butter(4,force_filter/500); CoP_R = filtfilt(b,a,CoP_R);

    torque = -rel_angle/204.8/.75*20*.308;

    %% EMG
    RF = EMG(:,1); VL = EMG(:,2); MH = EMG(:,3); AL = EMG(:,4);
    RF = abs(RF - mean(RF));VL = abs(VL - mean(VL));MH = abs(MH - mean(MH));AL = abs(AL - mean(AL));
    %     [b,a] = butter(8,emg_filter_1/500); RF = filtfilt(b,a,RF); [b,a] = butter(3,emg_filter_2/500); RF = filtfilt(b,a,RF);
    %     [b,a] = butter(8,emg_filter_1/500); VL = filtfilt(b,a,VL); [b,a] = butter(3,emg_filter_2/500); VL = filtfilt(b,a,VL);
    %     [b,a] = butter(8,emg_filter_1/500); MH = filtfilt(b,a,MH); [b,a] = butter(3,emg_filter_2/500); MH = filtfilt(b,a,MH);
    %     [b,a] = butter(8,emg_filter_1/500); AL = filtfilt(b,a,AL); [b,a] = butter(3,emg_filter_2/500); AL = filtfilt(b,a,AL);

    %     RF_f = bandstop(RF,1/dt_anc,59,61,4); RF = bandpass(RF_f,1/dt_anc,20,400);
    %     MH_f = bandstop(MH,1/dt_anc,59,61,4); MH = bandpass(MH_f,1/dt_anc,20,400);
    %     VL_f = bandstop(VL,1/dt_anc,59,61,4); VL = bandpass(VL_f,1/dt_anc,20,400);
    %     AL_f = bandstop(AL,1/dt_anc,59,61,4); AL = bandpass(AL_f,1/dt_anc,20,400);
    %%
    %x-direction is in line with  treadmill
    max_xdata = [RAnkle_lat(:,1) LAnkle_lat(:,1) RToe(:,1) LToe(:,1)]/1000;
    max_ydata = [RToe(:,2) LToe(:,2) RHeel(:,2) LHeel(:,2)]/1000;
    max_zdata = [zeros(length(Sacrum),1) Sacrum(:,3) Sac_offset(:,3)]/1000;

    %% Parsing motion data
    % LASIS,RASIS,Sacrum,Sac_offset,LThigh_up,LThigh_front,LThigh_rear,LKnee_lat,LKnee_med,...
    %     LShank_up, LShank_front, LShank_rear, LAnkle_lat,LAnkle_med, LHeel, LToe, RThigh_up,RThigh_front,RThigh_rear,RKnee_lat,RKnee_med,...
    %     RShank_up, RShank_front, RShank_rear, RAnkle_lat,RAnkle_med, RHeel, RToe, RTrochanter, LTrochanter]
    xdata = [LASIS(:,1),RASIS(:,1),Sacrum(:,1),LThigh_up(:,1),LThigh_front(:,1),LThigh_rear(:,1),...
        LShank_up(:,1), LShank_front(:,1), LShank_rear(:,1), LAnkle_lat(:,1), LHeel(:,1),  LToe(:,1), RThigh_up(:,1),RThigh_front(:,1),RThigh_rear(:,1),...
        RShank_up(:,1), RShank_front(:,1), RShank_rear(:,1), RAnkle_lat(:,1), RHeel(:,1),  RToe(:,1), Sac_offset(:,1)]/1000;

    ydata = [LASIS(:,2),RASIS(:,2),Sacrum(:,2),LThigh_up(:,2),LThigh_front(:,2),LThigh_rear(:,2),...
        LShank_up(:,2), LShank_front(:,2), LShank_rear(:,2), LAnkle_lat(:,2), LHeel(:,2), LToe(:,2), RThigh_up(:,2),RThigh_front(:,2),RThigh_rear(:,2),...
        RShank_up(:,2), RShank_front(:,2), RShank_rear(:,2), RAnkle_lat(:,2),RHeel(:,2), RToe(:,2), Sac_offset(:,2)]/1000;

    zdata = [LASIS(:,3),RASIS(:,3),Sacrum(:,3),LThigh_up(:,3),LThigh_front(:,3),LThigh_rear(:,3),...
        LShank_up(:,3), LShank_front(:,3), LShank_rear(:,3), LAnkle_lat(:,3), LHeel(:,3), LToe(:,3), RThigh_up(:,3),RThigh_front(:,3),RThigh_rear(:,3),...
        RShank_up(:,3), RShank_front(:,3), RShank_rear(:,3), RAnkle_lat(:,3), RHeel(:,3), RToe(:,3), Sac_offset(:,3)]/1000;
    [b,a] = butter(5,MA_filter/50); %100 Hz sampling rate
    xdata = filtfilt(b,a,xdata); ydata = filtfilt(b,a,ydata); zdata = filtfilt(b,a,zdata);

    %% Finding Heel Strike/Toe off
    [LHS,LHS_locs] = findpeaks(xdata(:,12)); [RHS,RHS_locs] = findpeaks(xdata(:,21)); RHS_locs = [1 RHS_locs];

    [LTO,LTO_locs] = findpeaks(-xdata(:,12)); [RTO,RTO_locs] = findpeaks(-xdata(:,21));

    if length(RHS_locs) == length(RTO_locs)
        RHS_locs = [RHS_locs length(xdata(:,12))];
    end
    [Max_LToe Max_LToe_ind] = max(zdata(:,12)); [Max_RToe Max_RToe_ind] = max(zdata(:,21));
    % Rankle_angle = atan2(RToe(:,3)-RHeel(:,3),RToe(:,1)-RHeel(:,1));

    %% Kinematics


    rthigh_angle = atan2(zdata(:,14)-zdata(:,15),xdata(:,14)-xdata(:,15));
    rshank_angle = atan2(zdata(:,17)-zdata(:,18),xdata(:,17)-xdata(:,18));
    rknee_angle = rthigh_angle-rshank_angle;
    rknee_vel = diff(rknee_angle)/dt_trc;
    [b,a] = butter(5,MA_filter/50); %100 Hz sampling rate
    rknee_vel = filtfilt(b,a,rknee_vel);

    lthigh_angle = atan2(zdata(:,5)-zdata(:,6),xdata(:,5)-xdata(:,6));
    lshank_angle = atan2(zdata(:,8)-zdata(:,9),xdata(:,8)-xdata(:,9)) ;
    lknee_angle = lthigh_angle-lshank_angle;
    lknee_vel = diff(lknee_angle)/dt_trc;
    [b,a] = butter(5,MA_filter/50); %100 Hz sampling rate
    lknee_vel = filtfilt(b,a,lknee_vel);

    RKnee_0 = [566.716610000000, -426.814480000000, 542.484860000000]/1000;
    LKnee_0 = [84.3858600000000,577.924440000000,544.943790000000];

    CoR_matrix = ...  % First line is for othosis case, second line is for no orthosis case
  [-0.0579198221993712,-0.200439601349412,-0.00947742297543508,1;0.104378980230751,-0.119563758507767,0.0308885445008167,1;-0.255171768559219,-0.0955733099187323,0.0957862576428981,1;-0.0829712127811288,0.269415876714610,0.0874703152530054,1;0.0783712596718107,0.151043165408165,0.0766265818486427,1;-0.171803255971511,0.149263606121605,-0.00340284181716344,1;-0.0324716092056170,0.216466706106638,-0.141588198922090,1;-0.194603484971473,0.171171431161309,0.140405494299697,1;0.201385668922983,-0.219567616567780,-0.0245618442347206,1;0.0948080667026759,-0.0258309685581289,0.0310757419435045,1;0.168748209389324,-0.0962542824874005,-0.243451518907812,1;0.0812828352005434,-0.0299538348121009,0.177638734761564,1;-0.0165182907668726,0.00869554469300227,0.0915803015493466,1;-0.0235134461831713,0.000826089836810373,-0.0856998037543076,1;]; 
    LHip_1 = CoR_matrix(1,:);
    RHip_1 = CoR_matrix(2,:);
    LHip_2 = CoR_matrix(3,:);
    RHip_2 = CoR_matrix(4,:);
    LKnee_1 = CoR_matrix(5,:);
    RKnee_1 = CoR_matrix(6,:);
    LKnee_in_1 = CoR_matrix(7,:);
    RKnee_in_1 = CoR_matrix(8,:);
    LKnee_2 = CoR_matrix(9,:);
    RKnee_2 = CoR_matrix(10,:);
    LKnee_in_2 = CoR_matrix(11,:);
    RKnee_in_2 = CoR_matrix(12,:);
    Lank = CoR_matrix(13,:);
    Rank = CoR_matrix(14,:);

    %% Extra Body Coords
    l_firstmet = [xdata(:,12) ydata(:,12)-foot_width zdata(:,12)];
    l_midmet = [xdata(:,12) ydata(:,12)-foot_width/2 zdata(:,12)];
    r_firstmet = [xdata(:,21) ydata(:,21)+foot_width zdata(:,21)];
    r_midmet = [xdata(:,21) ydata(:,21)+foot_width/2 zdata(:,21)];

    if any(trial == [5 9 13 16])
        ending = 11;
    else
        ending = 11;
    end
    for i = 1:RHS_locs(ending)%length(time_trc)
        %% Pelvis Coords
        Pelvis_rot = coord_def([xdata(i,3) ydata(i,3) zdata(i,3)], [xdata(i,1) ydata(i,1) zdata(i,1)], [xdata(i,2) ydata(i,2) zdata(i,2)]);
        RHip_p_CoR = Pelvis_rot*RHip_2';
        LHip_p_CoR = Pelvis_rot*LHip_2';

        %% Right Hip Coords
        RThigh_rot = coord_def([xdata(i,13) ydata(i,13) zdata(i,13)], [xdata(i,14) ydata(i,14) zdata(i,14)], [xdata(i,15) ydata(i,15) zdata(i,15)]);
        RKnee_CoR = RThigh_rot*RKnee_1';
        RKnee_CoR_med = RThigh_rot*RKnee_in_1';
        RHip_CoR = RThigh_rot*RHip_1';

        %% Right Knee Coords
        RShank_rot = coord_def([xdata(i,16) ydata(i,16) zdata(i,16)],[xdata(i,17) ydata(i,17) zdata(i,17)],[xdata(i,18) ydata(i,18) zdata(i,18)]);
        RKnee_CoR_shank =  RShank_rot*RKnee_2';
        RKnee_CoR_shank_med = RShank_rot*RKnee_in_2';

        %% Right Ankle Coords
        Rank_rot = coord_def([xdata(i,19) ydata(i,19) zdata(i,19)], [xdata(i,20) ydata(i,20) zdata(i,20)],[xdata(i,21) ydata(i,21) zdata(i,21)]);
        Rank_CoR = Rank_rot*Rank';

        %% joint coordinates
        % Determining coordinates for right hip
        RHip = RHip_p_CoR - 0.5*(RKnee_CoR - RKnee_CoR_med);
        %  Determining coordinates for right knee
        Mid_1 = 0.5*(RKnee_CoR - RKnee_CoR_med) + RKnee_CoR_med;
        Mid_2 = 0.5*(RKnee_CoR_shank - RKnee_CoR_shank_med) + RKnee_CoR_shank_med;
        RKnee = 0.5*(Mid_1 - Mid_2) + Mid_2;
        % Determining coordinates for right ankle
        RAnkle = 0.5*([xdata(i,19) ydata(i,19) zdata(i,19) 1]' - Rank_CoR) + Rank_CoR;  % finding midpoint between medial and lateral ankles

        %% Calculating left hip coords
        [LThigh_rot, x1,y1,z1] = coord_def([xdata(i,4) ydata(i,4) zdata(i,4)], [xdata(i,5) ydata(i,5) zdata(i,5)],[xdata(i,6) ydata(i,6) zdata(i,6)]);
        LKnee_CoR = LThigh_rot*LKnee_1';
        LKnee_CoR_med = LThigh_rot*LKnee_in_1';
        LHip_CoR = LThigh_rot*LHip_1';

        %% Calculating Left knee coords
        [LShank_rot, x1,y1,z1] = coord_def([xdata(i,7) ydata(i,7) zdata(i,7)], [xdata(i,8) ydata(i,8) zdata(i,8)],[xdata(i,9) ydata(i,9) zdata(i,9)]);
        LKnee_CoR_shank = LShank_rot*LKnee_2';
        LKnee_CoR_shank_med = LShank_rot*LKnee_in_2';

        %% Left Ankle Coords
        Lank_rot = coord_def([xdata(i,10) ydata(i,10) zdata(i,10)], [xdata(i,11) ydata(i,11) zdata(i,11)],[xdata(i,12) ydata(i,12) zdata(i,12)]);
        Lank_CoR = Lank_rot*Lank';

        %% joint coordinates
        % Determining coordinates for right hip
        LHip = LHip_p_CoR - 0.5*(LKnee_CoR - LKnee_CoR_med);
        %  Determining coordinates for right knee
        %         Mid_1 = 0.5*(LKnee_CoR - LKnee_CoR_med) + LKnee_CoR_med;
        Mid_2 = 0.5*(LKnee_CoR_shank - LKnee_CoR_shank_med) + LKnee_CoR_shank_med;
        %         LKnee = 0.5*(Mid_1 - Mid_2) + Mid_2;
        LKnee = Mid_2;
        % Determining coordinates for right ankle
        LAnkle = 0.5*([xdata(i,10) ydata(i,10) zdata(i,10) 1]' - Lank_CoR) + Lank_CoR;  % finding midpoint between medial and lateral ankles

        %% calculating left knee angle
        left_thigh_angle(i) = atan2(LHip(3)-LKnee(3),LHip(1)- LKnee(1));
        left_shank_angle(i) = atan2(LKnee(3)-LAnkle(3),LKnee(1)-LAnkle(1));

        %% calculating left hip flexion angle
        left_pelvis(i) = atan2(LASIS(i,3)/1000-LHip(3),LASIS(i,1)/1000- LHip(1));

        %% calculating left hip circumduction
        left_thigh_frontal(i) = atan2(LHip(3)-LKnee(3),LHip(2)- LKnee(2));
        left_pelvis_frontal(i) = atan2(LASIS(i,3)/1000-LHip(3),LASIS(i,2)/1000- LHip(2));

        %% calculating right knee angle
        right_thigh_angle(i) = atan2(RHip(3)-RKnee(3),RHip(1)- RKnee(1));
        right_shank_angle(i) = atan2(RKnee(3)-RAnkle(3),RKnee(1)- RAnkle(1));

        %% calculating right knee angle
        right_thigh_angle(i) = atan2(RHip(3)-RKnee(3),RHip(1)- RKnee(1));
        right_shank_angle(i) = atan2(RKnee(3)-RAnkle(3),RKnee(1)-RAnkle(1));

        %% calculating right hip flexion angle
        right_pelvis(i) = atan2(RASIS(i,3)/1000-RHip(3),RASIS(i,1)/1000- RHip(1));

        %% calculating right hip circumduction
        right_thigh_frontal(i) = atan2(RHip(3)-RKnee(3),RHip(2)- RKnee(2));
        right_pelvis_frontal(i) = atan2(RASIS(i,3)/1000-RHip(3),RASIS(i,2)/1000- RHip(2));

        %% calculating pelvic tilt
        pelvic_tilt(i) = atan2(RASIS(i,3)/1000 - LASIS(i,3)/1000,RASIS(i,2)/1000 - LASIS(i,2)/1000);

                %% calculating right foot angle (sagittal)
        right_foot_angle(i) = atan2(RAnkle(3) - zdata(i,21),RAnkle(1) - xdata(i,21));
        %% calculating left foot angle (sagittal)
        left_foot_angle(i) = atan2(LAnkle(3) - zdata(i,12),LAnkle(1) - xdata(i,12));
        
    end
    l_steptime = diff(LHS_locs);
    r_steptime = diff(RHS_locs);
    r_steplength = xdata(RHS_locs(2:ending),20) - xdata(RTO_locs(1:ending-1),20);
    l_steplength = xdata(LHS_locs(1:ending-1),11) - xdata(LTO_locs(1:ending-1),11);

    alt_ending = ending-1;
    LTO_int_locs =  LTO_locs(2:alt_ending) - LHS_locs(1:alt_ending-1);
    LTO_norm_locs = floor(LTO_int_locs./l_steptime(1:length(LTO_int_locs))*100);
    RTO_int_locs =  RTO_locs(1:alt_ending) - RHS_locs(1:alt_ending);
    RTO_norm_locs = floor(RTO_int_locs./r_steptime(1:length(RTO_int_locs))*100);


    l_knee_total = left_thigh_angle - left_shank_angle;
    l_knee_vel_total = diff(l_knee_total)/dt_trc;
    [b,a] = butter(4,8/50); l_knee_vel_total = filtfilt(b,a,l_knee_vel_total')';
    if any(trial == catch_trials)
        lknee_vel_TO(trial) = mean(l_knee_vel_total(LTO_locs(2:ending-2)));
        lknee_vel_TO_catch(trial) = mean(l_knee_vel_total(LTO_locs(ending-1)));
    else
        lknee_vel_TO(trial) = mean(l_knee_vel_total(LTO_locs(2:ending-1)));
    end
l_ankle_total = left_shank_angle - left_foot_angle;

    r_knee_total = right_thigh_angle - right_shank_angle;
    r_knee_vel_total = diff(r_knee_total)/dt_trc;
    [b,a] = butter(4,8/50); r_knee_vel_total = filtfilt(b,a,r_knee_vel_total')';
    if any(trial == catch_trials)
        rknee_vel_TO(trial) = mean(r_knee_vel_total(RTO_locs(2:ending-2)));
        rknee_vel_TO_catch(trial) = mean(r_knee_vel_total(RTO_locs(ending-1)));
    else
        rknee_vel_TO(trial) = mean(r_knee_vel_total(RTO_locs(2:ending-1)));
    end
    interplength = 2000;
r_ankle_total = right_shank_angle - right_foot_angle;

    for i = 2:ending-1%length(LHS)

        percent_s = (time_trc(LHS_locs(i-1):LHS_locs(i))-time_trc(LHS_locs(i-1)))...
            /(time_trc(LHS_locs(i))-time_trc(LHS_locs(i-1)));
        percent_s_EMG = (time_anc(10*LHS_locs(i-1):10*LHS_locs(i))-10*time_anc(LHS_locs(i-1)))...
            /(time_anc(10*LHS_locs(i))-time_anc(10*LHS_locs(i-1)));

        l_thigh_s = left_thigh_angle(LHS_locs(i-1):LHS_locs(i)); l_thigh(i,:) = interp1(percent_s,l_thigh_s,linspace(0,1,interplength));
        l_shank_s = left_shank_angle(LHS_locs(i-1):LHS_locs(i)); l_shank(i,:) = interp1(percent_s,l_shank_s,linspace(0,1,interplength));
        l_toe_height_s = zdata(LHS_locs(i-1):LHS_locs(i),12);l_toe_height(i,:) = interp1(percent_s,l_toe_height_s,linspace(0,1,interplength));
        l_knee_s = l_knee_total(LHS_locs(i-1):LHS_locs(i)); l_knee(i,:) = interp1(percent_s,l_knee_s,linspace(0,1,interplength));
        l_ankle_s = l_ankle_total(LHS_locs(i-1):LHS_locs(i)); l_ankle(i,:) = interp1(percent_s,l_ankle_s,linspace(0,1,interplength));
        l_knee_vel_s = l_knee_vel_total(LHS_locs(i-1):LHS_locs(i)); l_knee_vel(i,:) = interp1(percent_s,l_knee_vel_s,linspace(0,1,interplength));
        l_knee_s_alt = lknee_angle(LHS_locs(i-1):LHS_locs(i)); l_knee_alt(i,:) = interp1(percent_s,l_knee_s_alt,linspace(0,1,interplength));
        l_knee_vel_s_alt = lknee_vel(LHS_locs(i-1):LHS_locs(i)); l_knee_vel_alt(i,:) = interp1(percent_s,l_knee_vel_s_alt,linspace(0,1,interplength));
        l_pelvis_front_s = left_pelvis_frontal(LHS_locs(i-1):LHS_locs(i));l_pelvis_frontal(i,:) =  interp1(percent_s,l_pelvis_front_s,linspace(0,1,interplength));
        l_pelvis_s = left_pelvis(LHS_locs(i-1):LHS_locs(i)); l_pelvis(i,:) =  interp1(percent_s,l_pelvis_s,linspace(0,1,interplength));
        l_thigh_front_s = left_thigh_frontal(LHS_locs(i-1):LHS_locs(i)); l_thigh_frontal(i,:) =  interp1(percent_s,l_thigh_front_s,linspace(0,1,interplength));
        l_GRF_mag_s = Lforce_mag(10*LHS_locs(i-1):10*LHS_locs(i)); l_GRF_mag(i,:) = interp1(percent_s_EMG,l_GRF_mag_s,linspace(0,1,interplength));
        l_GRF_dir_s = Lforce_dir(10*LHS_locs(i-1):10*LHS_locs(i),:); l_GRF_dir(i,:,:) = interp1(percent_s_EMG,l_GRF_dir_s,linspace(0,1,interplength));
        l_RF_s = RF(10*LHS_locs(i-1):10*LHS_locs(i)); l_RF(i,:) = interp1(percent_s_EMG,l_RF_s,linspace(0,1,interplength));
        l_MH_s = MH(10*LHS_locs(i-1):10*LHS_locs(i)); l_MH(i,:) = interp1(percent_s_EMG,l_MH_s,linspace(0,1,interplength));
        l_AL_s = AL(10*LHS_locs(i-1):10*LHS_locs(i)); l_AL(i,:) = interp1(percent_s_EMG,l_AL_s,linspace(0,1,interplength));
        l_VL_s = VL(10*LHS_locs(i-1):10*LHS_locs(i)); l_VL(i,:) = interp1(percent_s_EMG,l_VL_s,linspace(0,1,interplength));
        torque_s = torque(10*LHS_locs(i-1):10*LHS_locs(i)); l_torque(i,:) = interp1(percent_s_EMG,torque_s,linspace(0,1,interplength));
        l_circ(i,:) = l_pelvis_frontal(i,:)-l_thigh_frontal(i,:);
        l_hip(i,:) = l_pelvis(i,:) - l_thigh(i,:);
    end

    for i = 2:ending%length(RHS)
        percent_s = (time_trc(RHS_locs(i-1):RHS_locs(i))-time_trc(RHS_locs(i-1)))...
            /(time_trc(RHS_locs(i))-time_trc(RHS_locs(i-1)));
        percent_s_EMG = (time_anc(10*RHS_locs(i-1):10*RHS_locs(i))-10*time_anc(RHS_locs(i-1)))...
            /(time_anc(10*RHS_locs(i))-time_anc(10*RHS_locs(i-1)));

        r_thigh_s = right_thigh_angle(RHS_locs(i-1):RHS_locs(i)); r_thigh(i,:) = interp1(percent_s,r_thigh_s,linspace(0,1,interplength));
        r_shank_s = right_shank_angle(RHS_locs(i-1):RHS_locs(i)); r_shank(i,:) = interp1(percent_s,r_shank_s,linspace(0,1,interplength));
        r_toe_height_s = zdata(RHS_locs(i-1):RHS_locs(i),21); r_toe_height(i,:) = interp1(percent_s,r_toe_height_s,linspace(0,1,interplength));
        r_knee_s = rknee_angle(RHS_locs(i-1):RHS_locs(i)); r_knee(i,:) = interp1(percent_s,r_knee_s,linspace(0,1,interplength));
        r_ankle_s = r_ankle_total(RHS_locs(i-1):RHS_locs(i)); r_ankle(i,:) = interp1(percent_s,r_ankle_s,linspace(0,1,interplength));
        r_knee_vel_s = rknee_vel(RHS_locs(i-1):RHS_locs(i)); r_knee_vel(i,:) = interp1(percent_s,r_knee_vel_s,linspace(0,1,interplength));
        pelvic_tilt_s = pelvic_tilt(RHS_locs(i-1):RHS_locs(i)); p_tilt(i,:) = interp1(percent_s,pelvic_tilt_s,linspace(0,1,interplength));
        r_pelvis_front_s = right_pelvis_frontal(RHS_locs(i-1):RHS_locs(i)); r_pelvis_frontal(i,:) =  interp1(percent_s,r_pelvis_front_s,linspace(0,1,interplength));
        r_pelvis_s = right_pelvis(RHS_locs(i-1):RHS_locs(i));  r_pelvis(i,:) =  interp1(percent_s,r_pelvis_s,linspace(0,1,interplength));
        r_thigh_front_s = right_thigh_frontal(RHS_locs(i-1):RHS_locs(i)); r_thigh_frontal(i,:) =  interp1(percent_s,r_thigh_front_s,linspace(0,1,interplength));
        r_circ(i,:) = r_pelvis_frontal(i,:)-r_thigh_frontal(i,:);
        r_hip(i,:) = r_pelvis(i,:) - r_thigh(i,:);
        r_GRF_mag_s = Rforce_mag(10*RHS_locs(i-1):10*RHS_locs(i)); r_GRF_mag(i,:) = interp1(percent_s_EMG,r_GRF_mag_s,linspace(0,1,interplength));
        r_GRF_dir_s = Rforce_dir(10*RHS_locs(i-1):10*RHS_locs(i),:); r_GRF_dir(i,:,:) = interp1(percent_s_EMG,r_GRF_dir_s,linspace(0,1,interplength));
    end



    %% plotting
    x = linspace(0,100, interplength);
    [m n] = size(l_knee);
    if any (trial == catch_trials)
        m = m -1 ;
    end

    mean_lknee = mean(l_knee(2:m,interplength*0.4:interplength*0.9));
    mean_lankle = mean(l_ankle(2:m,:));
    mean_lknee_alt = mean(l_knee_alt(2:m,interplength*0.4:interplength*0.9));
    l_max_toe_height = max(mean(l_toe_height(2:m,interplength*0.4:interplength*0.9)));
    mean_lcirc = mean(l_circ(2:m,:));
    mean_lhip = mean(l_hip(2:m,:));
    mean_tilt = mean(p_tilt(2:m,:));
    mean_torque = mean(l_torque(2:m,:));
    mean_pelvis_lf = mean(l_pelvis_frontal(2:m,:));
    mean_ldir_int = mean(l_GRF_dir(2:m,:,:),1);
    mean_ldir(:,1) = mean_ldir_int(1,:,1); mean_ldir(:,2) = mean_ldir_int(1,:,2); mean_ldir(:,3) = mean_ldir_int(1,:,3);

    mean_lMH = mean(l_MH(2:m,:));mean_lRF = mean(l_RF(2:m,:));mean_lVL = mean(l_VL(2:m,:));mean_lAL = mean(l_AL(2:m,:));

    %% Knee flexion velocity
    %     l_knee_vel = diff(l_knee,1,2)/dt_trc ;
    %     [b,a] = butter(4,MA_filter/50); l_knee_vel = filtfilt(b,a,l_knee_vel')';
    mean_lknee_vel = mean(l_knee_vel(2:m,:));
    mean_lknee_vel_alt = mean(l_knee_vel_alt(2:m,:));


    [mr nr] = size(r_knee);
    if any (trial == catch_trials)
        mr = mr -1 ;
    end
    mean_rknee = mean(r_knee(2:mr,interplength*0.4:interplength*0.9));
    mean_rankle= mean(r_ankle(2:m,:));
    r_max_toe_height = max(mean(r_toe_height(2:mr,interplength*0.4:interplength*0.9)));
    mean_rcirc = mean(r_circ(2:mr,:));
    mean_rhip = mean(r_hip(2:mr,:));
    mean_pelvis_rf = mean(r_pelvis_frontal(2:mr,:));
    mean_rdir_int = mean(r_GRF_dir(2:mr,:,:),1);
    mean_rdir(:,1) = mean_rdir_int(1,:,1); mean_rdir(:,2) = mean_rdir_int(1,:,2); mean_rdir(:,3) = mean_rdir_int(1,:,3);

    %% Knee flexion velocity
    %     r_knee_vel = diff(r_knee,1,2)/dt_trc ;
    %     [b,a] = butter(4,MA_filter/50); r_knee_vel = filtfilt(b,a,r_knee_vel')';
    mean_rknee_vel = mean(r_knee_vel(2:mr,:));


    %  fh1 = figure(1);
    % plot(x, mean(l_RF(2:4,:)))
    % hold on;
    % plot(x, l_RF(5,:),'r')

    %     figure(4)
    %     subplot(5,1,1); plot(x,l_torque); ylabel('Torque');
    %     subplot(5,1,2); plot(x,l_RF);ylabel('RF');
    %     subplot(5,1,3); plot(x,l_VL);ylabel('VL');
    %     subplot(5,1,4); plot(x,l_MH); ylabel('MH');
    %     subplot(5,1,5); plot(x,l_AL); ylabel('AL');

    %% plotting knee range of motion
    figure(1);
    hold on;
    title('Knee Flexion')
    ylabel('Knee flexion angle (rad)')
    xlabel('Record #')
    if any (trial == [baseline washout])
        plot(trial,max(mean_lknee)-min(mean_lknee),'or')
        plot(trial,max(mean_rknee)-min(mean_rknee),'og')
        plot(trial,max(mean_lknee_alt)-min(mean_lknee_alt),'oc')

    else
        plot(trial,max(mean_lknee)-min(mean_lknee),'.r')
        plot(trial,max(mean_rknee)-min(mean_rknee),'.g')
        plot(trial,max(mean_lknee_alt)-min(mean_lknee_alt),'.c')

    end
    if any(trial == catch_trials)
        plot(trial,max(l_knee(6,:))-min(l_knee(6,:)),'or')
        plot(trial,max(r_knee(6,:))-min(r_knee(6,:)),'og')
        plot(trial,max(l_knee_alt(6,:))-min(l_knee_alt(6,:)),'oc')

    end

    % Storing knee flexion data
    data(sn).knee_angle.label = 'average range of motion ';
    data(sn).knee_angle.left.ROM(trial) = max(mean_lknee)-min(mean_lknee);
    data(sn).knee_angle.right.ROM(trial) = max(mean_rknee)-min(mean_rknee);
    data(sn).knee_angle.left_alt.ROM(trial) = max(mean_lknee_alt)-min(mean_lknee_alt);
    data(sn).knee_angle.left.average(:,trial) = single(mean(l_knee(2:m,:)));
    data(sn).knee_angle.right.average(:,trial) = single(mean(r_knee(2:mr,:)));
    data(sn).knee_angle.left_alt.average(:,trial) = single(mean(l_knee_alt(2:m,:)));
    data(sn).knee_angle.left_alt.note = 'Left Alt angle measured using thigh markers on left side';
    if any(trial == catch_trials)
        data(sn).knee_angle.left.ROM_catch(trial) =  max(l_knee(m,:))-min(l_knee(m,:));
        data(sn).knee_angle.right.ROM_catch(trial) =  max(r_knee(mr,:))-min(r_knee(mr,:));
        data(sn).knee_angle.left.ROM_catch_alt(trial) =  max(l_knee_alt(m,:))-min(l_knee_alt(m,:));
        data(sn).knee_angle.left.catch(:,trial) = single(l_knee(m,:));
        data(sn).knee_angle.right.catch(:,trial) = single(r_knee(mr,:));
        data(sn).knee_angle.left.catch_alt(:,trial) = single(l_knee_alt(m,:));
    end


    %% plotting knee velocity
    figure(2);
    hold on;
    title('Knee Flexion Velocity')
    ylabel('Knee flexion vel (rad/s)')
    xlabel('Record #')
    if any (trial == [baseline washout])
        plot(trial,max(mean_lknee_vel),'or')
        plot(trial,max(mean_rknee_vel),'og')
        plot(trial,max(mean_lknee_vel_alt),'oc')

    else
        plot(trial,max(mean_lknee_vel),'.r')
        plot(trial,max(mean_rknee_vel),'.g')
        plot(trial,max(mean_lknee_vel_alt),'.c')

    end
    if any(trial == catch_trials)
        plot(trial,max(l_knee_vel(m,:)),'or')
        plot(trial,max(r_knee_vel(mr,:)),'og')
        plot(trial,max(l_knee_vel_alt(m,:)),'oc')

    end
    data(sn).knee_vel.label = 'Knee flexion velocity during gait';
    data(sn).knee_vel.left.max(trial) = max(mean_lknee_vel);
    data(sn).knee_vel.right.max(trial) = max(mean_rknee_vel);
    data(sn).knee_vel.left_alt.max(trial) = max(mean_lknee_vel_alt);
    data(sn).knee_vel.left.average(:,trial) = single(mean(l_knee_vel(2:m,:)));
    data(sn).knee_vel.right.average(:,trial) = single(mean(r_knee_vel(2:mr,:)));
    data(sn).knee_vel.left_alt.average(:,trial) = single(mean(l_knee_vel_alt(2:m,:)));
    data(sn).knee_vel.left_alt.note = 'Left Alt angle measured using thigh markers on left side';
    if any(trial == catch_trials)
        data(sn).knee_vel.left.catch_max(trial) =  max(l_knee_vel(m,:));
        data(sn).knee_vel.right.catch_max(trial) =  max(r_knee_vel(mr,:));
        data(sn).knee_vel.left.catch_max_alt(trial) =  max(l_knee_vel_alt(m,:));
        data(sn).knee_vel.left.catch(:,trial) = single(l_knee_vel(m,:));
        data(sn).knee_vel.right.catch(:,trial) = single(r_knee_vel(mr,:));
        data(sn).knee_vel.left.catch_alt(:,trial) = single(l_knee_vel_alt(m,:));
    end



    figure(3);
    hold on;
    title('Knee Flexion Velocity at Toe-off')
    ylabel('Knee flexion vel (rad/s)')
    xlabel('Record #')
    if any (trial == [baseline washout])
        plot(trial,lknee_vel_TO(trial),'or')
        plot(trial,rknee_vel_TO(trial),'og')
    else
        plot(trial,lknee_vel_TO(trial),'.r')
        plot(trial,rknee_vel_TO(trial),'.g')
    end
    if any (trial == catch_trials)
        plot(trial,lknee_vel_TO_catch(trial),'or')

    end

    data(sn).knee_vel.label = 'Knee Flexion Velocity at Toe-off';
    data(sn).knee_vel_TO.left.normal(trial) = lknee_vel_TO(trial);
    data(sn).knee_vel_TO.right.normal(trial) = rknee_vel_TO(trial);
    if any(trial == catch_trials)
        data(sn).knee_vel_TO.left.catch(trial) =  lknee_vel_TO_catch(trial);
        data(sn).knee_vel_TO.right.catch(trial) =  rknee_vel_TO_catch(trial);
    end


    %% plotting hip range of motion
    figure(9);
    hold on;
    title('Hip Flexion')
    ylabel('Hip Flexion Angle (rad)')
    xlabel('Record #')
    if any (trial == [baseline washout])
        plot(trial,max(mean_lhip)-min(mean_lhip),'or')
        plot(trial,max(mean_rhip)-min(mean_rhip),'og')
    else
        plot(trial,max(mean_lhip)-min(mean_lhip),'.r')
        plot(trial,max(mean_rhip)-min(mean_rhip),'.g')
    end
    if any(trial == catch_trials)
        plot(trial,max(l_hip(m,:))-min(l_hip(m,:)),'or')
        plot(trial,max(r_hip(mr,:))-min(r_hip(mr,:)),'og')
    end

    data(sn).hip_angle.label = 'Hip Sagittal Plane Motion';
    data(sn).hip_angle.left.ROM(trial) = max(mean_lhip)-min(mean_lhip);
    data(sn).hip_angle.right.ROM(trial) = max(mean_rhip)-min(mean_rhip);
    data(sn).hip_angle.left.average(:,trial) = single(mean(l_hip(2:m,:)));
    data(sn).hip_angle.right.average(:,trial) = single(mean(r_hip(2:mr,:)));

    if any(trial == catch_trials)
        data(sn).hip_angle.left.catch_ROM(trial) =  max(l_hip(m,:))-min(l_hip(m,:));
        data(sn).hip_angle.right.catch_ROM(trial) = max(r_hip(mr,:))-min(r_hip(mr,:));
        data(sn).hip_angle.left.catch(:,trial) =  single(l_hip(m,:));
        data(sn).hip_angle.right.catch(:,trial) = single(r_hip(mr,:));
    end




    % plot(x, mean(l_knee(1:3,:)),'g')
    % hold on;
    % plot(x,mean(l_knee(1:3,:))+std(l_knee(1:3,:)),'b')

    % plotting hip circumduction before and after training
    figure(5);
    title('Hip Circumduction')
    ylabel('Circumduction (rad)')
    xlabel('Record #')
    hold on;
    if any (trial == [baseline washout])
        plot(trial,max(mean_lcirc)-min(mean_lcirc),'or')
        plot(trial,max(mean_rcirc)-min(mean_rcirc),'og')
    else
        plot(trial,max(mean_lcirc)-min(mean_lcirc),'.r')
        plot(trial,max(mean_rcirc)-min(mean_rcirc),'.g')
    end
    if any(trial == catch_trials)
        plot(trial,max(l_circ(m,:))-min(l_circ(m,:)),'or')
        plot(trial,max(r_circ(mr,:))-min(r_circ(mr,:)),'og')
    end

    data(sn).hip_circ.label = 'Hip Circumduction Angle';
    data(sn).hip_circ.left.ROM(trial) = max(mean_lcirc)-min(mean_lcirc);
    data(sn).hip_circ.right.ROM(trial) = max(mean_rcirc)-min(mean_rcirc);
    data(sn).hip_circ.left.average(:,trial) = single(mean_lcirc);
    data(sn).hip_circ.right.average(:,trial) = single(mean_rcirc);

    if any(trial == catch_trials)
        data(sn).hip_circ.left.catch_ROM(trial) =  max(l_circ(m+1,:))-min(l_circ(m+1,:));
        data(sn).hip_circ.right.catch_ROM(trial) = max(r_circ(mr+1,:))-min(r_circ(mr+1,:));
        data(sn).hip_circ.left.catch(:,trial) =  single(l_circ(m+1,:));
        data(sn).hip_circ.right.catch(:,trial) = single(r_circ(mr+1,:));
    end
    
        % plotting pelvic circumduction before and after training
    figure(19); 
    title('Pelvic Circ')
    ylabel('Circumduction (rad)')
    xlabel('Record #')
    hold on;
    if any (trial == [baseline washout])
        plot(trial,max(mean_pelvis_lf)-min(mean_pelvis_lf)+ data(sn).hip_circ.left.ROM(trial),'or')
        plot(trial,max(mean_pelvis_rf)-min(mean_pelvis_rf) + data(sn).hip_circ.right.ROM(trial),'og')
    else
        plot(trial,max(mean_pelvis_lf)-min(mean_pelvis_lf) + data(sn).hip_circ.left.ROM(trial),'.r')
        plot(trial,max(mean_pelvis_rf)-min(mean_pelvis_rf) + data(sn).hip_circ.right.ROM(trial),'.g')
    end
    if any(trial == catch_trials)
        plot(trial,max(l_pelvis_frontal(m+1,:))-min(l_pelvis_frontal(m+1,:))+ data(sn).hip_circ.left.catch_ROM(trial),'or')
        plot(trial,max(r_pelvis_frontal(mr+1,:))-min(r_pelvis_frontal(mr+1,:))+data(sn).hip_circ.right.catch_ROM(trial),'og')
    end

    data(sn).pelvic_circ.label = 'Pelvic Circumduction Angle';
    data(sn).pelvic_circ.left.ROM(trial) = max(mean_pelvis_lf)-min(mean_pelvis_lf);
    data(sn).pelvic_circ.right.ROM(trial) = max(mean_pelvis_rf)-min(mean_pelvis_rf);
    data(sn).pelvic_circ.left.average(:,trial) = single(mean_pelvis_lf);
    data(sn).pelvic_circ.right.average(:,trial) = single(mean_pelvis_rf);

    if any(trial == catch_trials)
        data(sn).pelvic_circ.left.catch_ROM(trial) =  max(l_pelvis_frontal(m+1,:))-min(l_pelvis_frontal(m+1,:));
        data(sn).pelvic_circ.right.catch_ROM(trial) = max(r_pelvis_frontal(mr+1,:))-min(r_pelvis_frontal(mr+1,:));
        data(sn).pelvic_circ.left.catch(:,trial) =  single(l_pelvis_frontal(m+1,:));
        data(sn).pelvic_circ.right.catch(:,trial) = single(r_pelvis_frontal(mr+1,:));
    end


    % plotting spatiotemporal patterns
    figure(10)
    hold on;
    title('Step Length')
    ylabel('Step Length (m)')
    xlabel('Record #')
    plot(trial,mean(l_steplength),'.r')
    plot(trial,mean(r_steplength),'.g')

    data(sn).steplength.label = 'Step Length (Toe off to Heel strike)';
    if any(trial == catch_trials)
        n = ending-2;
        data(sn).steplength.left.catch(trial) = single(l_steplength(n+1));
        data(sn).steplength.right.catch(trial) = single(r_steplength(n+1));
        data(sn).steplength.left.normal(trial) = single(mean(l_steplength(1:n)));
        data(sn).steplength.right.normal(trial) = single(mean(r_steplength(1:n)));
    else
        n = ending-1;
        data(sn).steplength.left.normal(trial) = single(mean(l_steplength(1:n)));
        data(sn).steplength.right.normal(trial) = single(mean(r_steplength(1:n)));
    end

    figure(12);
    hold on;

    title('Toe Clearance')
    ylabel('Max Toe height (m)')
    xlabel('Record #')
    plot(trial,mean(l_max_toe_height),'.r')
    plot(trial,mean(r_max_toe_height),'.g')
    if any (trial == catch_trials)
        plot(trial,max(l_toe_height(m,interplength*0.4:interplength*0.9)),'or')
        plot(trial,max(r_toe_height(mr,interplength*0.4:interplength*0.9)),'og')
    end

    % Toe Clearance Data
    data(sn).toeclear.label = 'Toe Clearance from 40-90% G.C.';
    if any(trial == catch_trials)
        n = ending-2;
        data(sn).toeclear.left.catch(trial) = single(max(l_toe_height(n+1,interplength*0.4:interplength*0.9)));
        data(sn).toeclear.right.catch(trial) = single(max(r_toe_height(n+1,interplength*0.4:interplength*0.9)));
        data(sn).toeclear.left.normal(trial) = single(l_max_toe_height);
        data(sn).toeclear.right.normal(trial) = single(r_max_toe_height);
    else
        n = ending-1;
        data(sn).toeclear.left.normal(trial) = single(l_max_toe_height);
        data(sn).toeclear.right.normal(trial) = single(r_max_toe_height);
    end

    % Storing Torque
    data(sn).torque.label = 'Maximum torque averaged over steps';
    data(sn).torque.max(trial) = single(max(mean_torque));
    data(sn).torque.average(:,trial) = single(mean_torque);
    if any(trial == catch_trials)
        data(sn).torque.catch_max(trial) =  single(max(l_torque(m,:)));
        data(sn).torque.catch_average(:,trial) =  single(l_torque(m,:));
    end

    %GRF Magnitude
    data(sn).GRF.mag.label = 'Ground Reaction Force Magnitude';
    data(sn).GRF.mag.right.average(:,trial) = single(mean(r_GRF_mag(2:mr,:)));
    data(sn).GRF.mag.left.average(:,trial) = single(mean(l_GRF_mag(2:m,:)));
    if any(trial == catch_trials)
        data(sn).GRF.mag.right.catch(:,trial) =  single(r_GRF_mag(mr,:));
        data(sn).GRF.mag.left.catch(:,trial) =  single(l_GRF_mag(m,:));
    end

    %GRF Direction
    data(sn).GRF.dir.label = 'Ground Reaction Force Direction (x-dir is forward, RH coords)';
    data(sn).GRF.dir.right.trial(trial).average = single(mean_rdir);
    data(sn).GRF.dir.left.trial(trial).average = single(mean_ldir);
    if any(trial == catch_trials)
        mean_ldir_catch_int = mean(l_GRF_dir(m,:,:),1);
        mean_ldir_catch(:,1) = mean_ldir_catch_int(1,:,1); mean_ldir_catch(:,2) = mean_ldir_catch_int(1,:,2); mean_ldir_catch(:,3) = mean_ldir_catch_int(1,:,3);
        mean_rdir_catch_int = mean(r_GRF_dir(mr,:,:),1);
        mean_rdir_catch(:,1) = mean_rdir_catch_int(1,:,1); mean_rdir_catch(:,2) = mean_rdir_catch_int(1,:,2); mean_rdir_catch(:,3) = mean_rdir_catch_int(1,:,3);

        data(sn).GRF.dir.right.trial(trial).catch =  single(mean_rdir_catch);
        data(sn).GRF.dir.trial(trial).left.catch =  single(mean_ldir_catch);
    end

    %EMG
    data(sn).EMG.label = 'Mean rectified EMG';
    data(sn).EMG.RF.typical(:,trial) = single(mean_lRF);
    data(sn).EMG.VL.typical(:,trial) = single(mean_lVL);
    data(sn).EMG.AL.typical(:,trial) = single(mean_lAL);
    data(sn).EMG.MH.typical(:,trial) = single(mean_lMH);
    if any(trial == catch_trials)
        data(sn).EMG.RF.catch(:,trial) = single(l_RF(m,:));
        data(sn).EMG.VL.catch(:,trial) = single(l_VL(m,:));
        data(sn).EMG.AL.catch(:,trial) = single(l_AL(m,:));
        data(sn).EMG.MH.catch(:,trial) = single(l_MH(m,:));
    end


    %Gait Event Timing

    data(sn).Foot_Off.label = 'Foot Off Time';
    data(sn).Foot_Off.right.average(trial) = single(mean(RTO_norm_locs(2:mr-1)));
    data(sn).Foot_Off.left.average(trial) = single(mean(LTO_norm_locs(2:m-1)));
    if any(trial == catch_trials)
        data(sn).Foot_Off.right.catch(trial) =  RTO_norm_locs(mr);
        data(sn).Foot_Off.left.catch(trial) =  LTO_norm_locs(m);
    end

    
    %ankle angle
    data(sn).ankle_angle.label = 'average ankle range of motion';
    data(sn).ankle_angle.left.ROM(trial) = max(mean_lankle)-min(mean_lankle);
    data(sn).ankle_angle.right.ROM(trial) = max(mean_rankle)-min(mean_rankle);
    data(sn).ankle_angle.left.average(:,trial) = single(mean(l_ankle(2:m,:)));
    data(sn).ankle_angle.right.average(:,trial) = single(mean(r_ankle(2:mr,:)));
    if any(trial == catch_trials)
        data(sn).ankle_angle.left.ROM_catch(trial) =  max(l_ankle(m,:))-min(l_ankle(1,:));
        data(sn).ankle_angle.right.ROM_catch(trial) =  max(r_ankle(mr,:))-min(r_ankle(1,:));
        data(sn).ankle_angle.left.catch(:,trial) = single(l_ankle(m,:));
        data(sn).ankle_angle.right.catch(:,trial) = single(r_ankle(mr,:));
    end


end


% look at GRF magnitude as outcome measure
% ankle angle as outcome measure
% hip flexion torque in early swing as outcome measure

% fix Kip's data

% save data_jim.mat  data;
%
% figure;
% plot(x,mean_lRF/max(mean_lRF),'r');
% hold on;
% plot(x,mean_lAL/max(mean_lAL),'k');
% plot(x,mean_lVL/max(mean_lVL));
% plot(x,mean_lMH/max(mean_lMH),'g');
% plot(x,mean_torque/max(mean_torque),'y')
